public class demo2 {
    //观察多线程的优势
    private static final long count = 10_0000_0000;
    public static void main(String[] args) throws InterruptedException {
        // 多线程
        concurrency();
        // 单线程
        serial();
    }

    private static void concurrency() {
        long begin = System.nanoTime();
        // 创建⼀个线程计算 a 的值
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int a = 0;
                for (long i = 0; i < count; i++) {
                    a--;
                }
            }
        });
        thread.start();
        // 主线程内计算 b 的值
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        // 统计耗时
        long end = System.nanoTime();
        double ms = (end - begin) * 1.0 / 1000 / 1000;
        System.out.println("多线程: "+ms+"毫秒");
    }

    private static void serial() {
// 全部在主线程内计算 a、b 的值
        long begin = System.nanoTime();
        int a = 0;
        for (long i = 0; i < count; i++) {
            a--;
        }
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long end = System.nanoTime();
        double ms = (end - begin) * 1.0 / 1000 / 1000;
        System.out.println("单线程: "+ms+"毫秒");

    }
}
